import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/gradient_card_view.dart';
import 'package:baselib/widgets/inkwell_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/comm_widget_manager.dart';
import 'package:framelib/widgets/main_gradient_button.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:framelib/widgets/typewidget/gift_type_model.dart';
import 'package:framelib/widgets/typewidget/gift_type_widget.dart';
import 'package:module_business/module_chat/viewmodel/gifts_bottom_sheet_view_model.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_index/model/voice_person_entity.dart';
import 'package:module_business/module_index/viewmodel/room_gifts_bottom_sheet_view_model.dart';

class RoomGiftsBottomSheetWidget extends GetView<RoomGiftsBottomSheetViewModel> {
  String yxRoomId;
  int roomId;
  List<UserEntity> defaultPersons;
  Function onSend;
  RoomGiftsBottomSheetWidget({
    Key key,
    this.yxRoomId,
    this.roomId,
    this.defaultPersons,
    this.onSend,
  }) : super(key: key);
  int selectedIndex = -1;

  @override
  Widget build(BuildContext context) {
    var tag = '${DateTime.now().millisecondsSinceEpoch}';
    Get.put(RoomGiftsBottomSheetViewModel(yxRoomId: yxRoomId,roomId: roomId,defaultPersons: defaultPersons), tag: tag);
    return PageWidget.build(
        tag: tag,
        builder: (RoomGiftsBottomSheetViewModel model) {
          return ListView(
            shrinkWrap: true,
            children: [
              //上麦人员
              Obx(() => Container(
                width: PxUtil.instance.screenWidth,
                height: PxUtil.instance.px_60,
                child: Row(
                  children: [
                    Expanded(child: ListView.builder(
                        padding: EdgeInsets.only(
                          left: PxUtil.instance.px_15,
                          right: PxUtil.instance.px_15,
                        ),
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        itemCount: model.selectedPersons.value.length??0,
                        itemBuilder: (BuildContext context,int index){
                          var voicePerson = model.selectedPersons.value[index];
                          return Container(
                            margin: EdgeInsets.only(
                                right: PxUtil.instance.px_12
                            ),
                            child: Stack(
                              alignment: Alignment.center,
                              children: [
                                CommWidgetManager.buildCicleImagWidget(
                                  voicePerson?.icon ?? '',
                                  placeholder:
                                  DrawableUtil.instance.icon_default_head,
                                  width: PxUtil.instance.px_36,
                                  height: PxUtil.instance.px_36,
                                  roundRadius: PxUtil.instance.px_28,
                                  backgroundColor: ColorUtil.instance.color_f2f2f2,
                                  padding: EdgeInsets.only(),
                                  margin: EdgeInsets.only(),
                                ),
                                Positioned(
                                    bottom: PxUtil.instance.px_0,
                                    left: PxUtil.instance.px_15,
                                    right: PxUtil.instance.px_15,
                                    child: GradientCardView(
                                      elevation: PxUtil.instance.px_0,
                                      alignment: Alignment.center,
                                      margin: EdgeInsets.only(),
                                      padding: EdgeInsets.only(),
                                      borderRadius: BorderRadius.circular(PxUtil.instance.px_2),
                                      child: Container(
                                        child: Text(
                                          '${(voicePerson?.fansNum??0)>999?'999+':(voicePerson?.fansNum??0)}',
                                          style: TextStyle(
                                              fontSize: PxUtil.instance.px_8,
                                              color: ColorUtil.instance.color_white
                                          ),
                                        ),
                                      ),
                                    )
                                ),
                              ],
                            ),
                          );
                        }
                    )),
                    InkWellButton(
                      onPressed: ()=>model.onPenAll(),
                      child: Container(
                        margin: EdgeInsets.only(
                          right: PxUtil.instance.px_15,
                        ),
                        child: GradientCardView(
                          elevation: PxUtil.instance.px_0,
                          gradient: LinearGradient(
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              colors: [
                                ColorUtil.instance.color_7776FF,
                                ColorUtil.instance.color_AD2CF1,
                              ]
                          ),
                          width: PxUtil.instance.px_26,
                          height: PxUtil.instance.px_26,
                          borderRadius: BorderRadius.circular(
                              PxUtil.instance.px_13),
                          alignment: Alignment.center,
                          child: Text(
                            StringUtil.instance.All,
                            style: TextStyle(
                                color: ColorUtil.instance.color_white,
                                fontSize: PxUtil.instance.px_11
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              )),
              Container(
                child: (model.giftsDatas??[]).isEmpty?SizedBox(width: PxUtil.instance.px_0,):
                GiftTypeWidget(
                  backGroundColor: ColorUtil.instance.color_transparent,
                  perHorNum: 4,
                  barWidth: 4,
                  barSelectedColor: ColorUtil.instance.color_white,
                  barUnSelectedColor: ColorUtil.instance.color_999999,
                  isShowNamePreImg: true,
                  datas: (model.giftsDatas??[]).map((e) => GiftTypeModel(
                      img: e?.giftSmallImg,
                      name: '${e?.giftPrice}'
                  )).toList(),
                  onItemTap: (int selectedIndexTemp){
                    LogUtil.d('---->selectedIndexTemp=$selectedIndexTemp');
                    selectedIndex = selectedIndexTemp;
                  },
                ),
              ),
              SizedBox(height: PxUtil.instance.px_20,),
              //底部按钮
              Row(
                children: [
                  SizedBox(width: PxUtil.instance.px_22,),
                  Container(
                    child: Text(
                      '${StringUtil.instance.Balance}:10000',
                      style: TextStyle(
                          color: ColorUtil.instance.color_white,
                          fontSize: PxUtil.instance.px_13
                      ),
                    ),
                  ),
                  Expanded(child: Text('')),
                  SizedBox(
                    width: 100,
                    height: 34,
                    child: TextField(
                      controller: model.numTextEditingController,
                      inputFormatters: [FilteringTextInputFormatter.allow(RegExp("[1-9]"))],
                      keyboardType: TextInputType.number,
                      style: TextStyle(
                        color: Colors.white
                      ),
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.only(
                          left: PxUtil.instance.px_15,
                          right: PxUtil.instance.px_15,
                        ),
                          border: OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0.5,
                                  color: Colors.white
                              )
                          ),
                        disabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0.5,
                                color: Colors.white
                            )
                        ),
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0.5,
                                color: Colors.white
                            )
                        ),
                        enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                                width: 0.5,
                                color: Colors.white
                            )
                        ),
                      ),
                    ),
                  ),
                  InkWellButton(
                    onPressed: (){
                      if(model.selectedPersons.value.isEmpty){
                        ToastUtil.toast(StringUtil.instance.Please_select+" "+StringUtil.instance.Onmic_Person);
                        return;
                      }
                      if(selectedIndex==-1){
                        ToastUtil.toast(StringUtil.instance.Please_select+" "+StringUtil.instance.Gifts);
                        return;
                      }
                      if(model.numTextEditingController.text.isEmpty){
                        ToastUtil.toast(StringUtil.instance.Please_input_gift_number);
                        return;
                      }
                      int diamondNumTemp = UserParamsModel.instance.userEntity?.diamondNum??0;
                      int giftPrice = (model.giftsDatas??[])[selectedIndex]?.giftPrice??0;
                      int giftNum = int.parse(model.numTextEditingController.text??'1');

                      if(diamondNumTemp<giftPrice*giftNum*model.selectedPersons.value.length){
                        ToastUtil.toast(StringUtil.instance.Please_recharge);
                        return;
                      }
                      if(onSend!=null)onSend((model.giftsDatas??[])[selectedIndex],giftNum,model.selectedPersons.value);
                    },
                    child: Container(
                      margin: EdgeInsets.only(
                        // left: PxUtil.instance.px_15,
                        // right: PxUtil.instance.px_15,
                      ),
                      child: GradientCardView(
                        elevation: PxUtil.instance.px_0,
                        gradient: LinearGradient(
                            begin: Alignment.topCenter,
                            end: Alignment.bottomCenter,
                            colors: [
                              ColorUtil.instance.color_7776FF,
                              ColorUtil.instance.color_AD2CF1,
                            ]
                        ),
                        padding: EdgeInsets.only(
                          left: PxUtil.instance.px_7,
                          right: PxUtil.instance.px_7,
                          top: PxUtil.instance.px_2,
                          bottom: PxUtil.instance.px_2,
                        ),
                        borderRadius: BorderRadius.circular(
                            PxUtil.instance.px_26),
                        child: Text(
                          StringUtil.instance.Send,
                          style: TextStyle(
                              color: ColorUtil.instance.color_white,
                              fontSize: PxUtil.instance.px_11
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: PxUtil.instance.px_22,),
                ],
              ),
              SizedBox(height: PxUtil.instance.px_25,)
            ],
          );
        });
  }
}
